package com.shujia.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo05MapValues {
  def main(args: Array[String]): Unit = {

    /**
     * mapValues：对KV格式RDD的value进行遍历处理
     */
    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo05MapValues")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuLineRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    // 提取name以及age
    val nameAgeRDD: RDD[(String, Int)] = stuLineRDD.map(line => (line.split(",")(1), line.split(",")(2).toInt))

    // 对每个学生的年龄 +2
    nameAgeRDD.mapValues(_ + 2).foreach(println)

  }

}
